<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\GoodsListSmall;
use App\Admin\Actions\Post\UserInfoSmall;
use App\Admin\Repositories\LiveBroadcastRoom;
use App\Http\Controllers\LiveStream\LiveStreamController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class LiveBroadcastRoomController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new LiveBroadcastRoom(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('live_broadcast_room_title', '直播间标题');
            $grid->column('live_broadcast_room_text', '直播间描述');
            $grid->column('live_broadcast_room_img', '直播间封面')->image();
            $grid->column('live_goods_id', '直播间商品');
            $grid->column('live_status', '直播状态');
            $grid->column('streaming_url', '直播推流地址');
            $grid->column('live_begin_time', '直播开始时间');
            $grid->column('live_end_time', '直播结束时间');
            $grid->column('','进入直播间')->display('进入直播间')->link(function (){
                return admin_url('live?id='.$this->id);
            });

//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new LiveBroadcastRoom(), function (Show $show) {
            $show->view('live/live_stream');
            $show->field('id');
            $show->field('live_broadcast_room_title');
            $show->field('live_broadcast_room_text');
            $show->field('live_broadcast_room_img');
            $show->field('live_goods_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Form::make(new LiveBroadcastRoom(), function (Form $form) {
            $form->display('id');
            $form->multipleSelectTable('uid','主播')->display('主播')->from(UserInfoSmall::make())->saving(function ($e) {
                return $e[0];
            })->rules('required',[
                'required'=>'主播必选'
            ]);;
            $form->text('live_broadcast_room_title', '直播间标题')->rules('required',[
                'required'=>'标题不可以为空'
            ]);
            $form->text('live_broadcast_room_text', '直播间描述')->rules('required',[
                'required'=>'直播间描述不可以为空'
            ]);
            $form->image('live_broadcast_room_img', '直播间封面')->disk('qiniu')->uniqueName()->saveFullUrl()->autoUpload()->rules('required',[
                'required'=>'直播间封面不可以为空'
            ]);;
            $form->multipleSelectTable('live_goods_id','商品')->display('商品')->from(GoodsListSmall::make())->saving(function ($e) {
                return $e[0];
            })->rules('required',[
                'required'=>'商品不可以为空'
            ]);
            $form->text('live_status', '直播状态（0未开始 1直播中 2已结束 3暂停）')->rules('regex:/^\d+$/|max:1',[
                'regex'   => '状态值格式错误，请根据提示填写',
                'max'     => '状态填写错误,请根据提示填写',
            ]);
            $form->hidden('streaming_url', '直播推流地址');
            $form->hidden('play_url', '直播播流地址');
            $form->datetime('live_begin_time', '直播开始时间')->rules('required',[
                'required'=>'开始时间必选'
            ]);;
            $form->datetime('live_end_time', '直播结束时间')->rules('required',[
                'required'=>'结束时间必选'
            ]);;
            $form->display('created_at');
            $form->display('updated_at');
            $form->submitted(function (Form $form){
                //调取生成 推流播流的接口 获取推流播流地址 并添加入库
                $res=new LiveStreamController();
                $url=$res->liveBroadcastUrl();
                $form->streaming_url = $url['push_url'];
                $form->play_url= $url['play_url']['hls'];
            });
        });
    }
}
